package main

import (
	"fmt"
	// "log"

	"go-emacs-module"
)

func init() {
	goem.ModuleInit = ModuleInit
}

var em goem.Module

func ModuleInit(em_ goem.Module) int {
	em = em_
	env := em.GetEnv()

	// log.Println("hehehe")
	test_0()

	test_auto_buildtin_vars()

	env.NonLocalExitClear()
	goem.Provide(env.Intern("goem-tests"))
	// goem.Provide0("goem-tests")
	return 0
}

func ev2go(v goem.Value) string {
	env := em.GetEnv()

	var rv string

	sv := v.SymVal()
	// symvalfn := env.Intern("symbol-value")
	// sv := env.Funcall(symvalfn, []goem.Value{v})
	tostrfn := env.Intern("prin1-to-string")

	switch sv.Type() {
	case goem.EVTypeInt:
		rv = fmt.Sprintf("int: [%v]", sv.ToInt())
	case goem.EVTypeString:
		rv = fmt.Sprintf("str: [%v]", sv.ToString())
	case goem.EVTypeFloat:
		rv = fmt.Sprintf("float: [%v]", sv.ToFloat())
	case goem.EVTypeSymbol:
		env.NonLocalExitClear()
		pv := env.Funcall(tostrfn, []goem.Value{sv})
		rv = "sym: " + pv.ToString()
	// case goem.EVTypeList:
	// env.NonLocalExitClear()
	// pv := env.Funcall(env.Intern("prin1-to-string"), []goem.Value{sv})
	// pvstr := pv.ToString()
	// rv = fmt.Sprintf("list: %v", strmaxsub(pvstr, 64))
	// case goem.EVTypeFunc:
	// env.NonLocalExitClear()
	// pv := env.Funcall(env.Intern("prin1-to-string"), []goem.Value{sv})
	// rv = fmt.Sprintf("func: %v", pv.ToString())
	case goem.EVTypeCons:
		rv = fmt.Sprintf("cons: ")
	case goem.EVTypeVector:
		rv = fmt.Sprintf("vector: ")
	case goem.EVTypeCharTable:
		env.NonLocalExitClear()
		pv := env.Funcall(env.Intern("prin1-to-string"), []goem.Value{sv})
		rv = fmt.Sprintf("chtab: %v", strmaxsub(pv.ToString(), 64))
	case goem.EVTypeHashTable:
		rv = fmt.Sprintf("HashTable: ")
	case goem.EVTypeFinalizer:
		rv = fmt.Sprintf("finalizer: ")
	case goem.EVTypeBuffer:
		rv = fmt.Sprintf("buffer: ")
	case goem.EVTypeWindow:
		rv = fmt.Sprintf("window: ")
	case goem.EVTypeFrame:
		rv = fmt.Sprintf("frame: ")
	case goem.EVTypeMarker:
		rv = fmt.Sprintf("marker: ")
	// case goem.EVTypeArray:
	// rv = fmt.Sprintf("array: ")
	// case goem.EVTypeUnknown:
	// rv = fmt.Sprintf("real unknown: ")
	default:
		env.NonLocalExitClear()
		pv := env.Funcall(env.Intern("prin1-to-string"), []goem.Value{v})
		rv = "unknown -> " + pv.ToString()
	}

	return rv
}

func strmaxsub(s string, maxlen int) string {
	if len(s) >= 64 {
		return s[0:maxlen]
	}
	return s
}

func main() {}
